#include "../include/pieview.h"
#include<QPainter>

PieView::PieView(QWidget *parent)
    : QAbstractItemView{parent}
{

}

QRegion PieView::itemRegion(QModelIndex index)
{
    QRegion region;
    if(index.column()==1)   //销售数量
        region=RegionList[index.row()];
    return region;
}

void PieView::setSelectionModel(QItemSelectionModel *selectionModel)
{
    selections=selectionModel;
}

QRect PieView::visualRect(const QModelIndex &index) const
{
    return QRect();
}

void PieView::scrollTo(const QModelIndex &index, ScrollHint hint)
{

}

QModelIndex PieView::indexAt(const QPoint &point) const
{
    QPoint newPoint(point.x(),point.y());
    QRegion region;
    foreach (region, RegionList) {
        if(region.contains(newPoint)){
            int row=RegionList.indexOf(region);
            QModelIndex index=this->model()->index(row,1,rootIndex());
            return index;
        }
    }
    return QModelIndex();
}

QModelIndex PieView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
    return QModelIndex();
}

int PieView::horizontalOffset() const
{
    return 0;
}

int PieView::verticalOffset() const
{
    return 0;
}

bool PieView::isIndexHidden(const QModelIndex &index) const
{
    return false;
}

void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
{

}

QRegion PieView::visualRegionForSelection(const QItemSelection &selection) const
{
    return QRegion();
}

void PieView::paintEvent(QPaintEvent *event)
{
    QPainter painter(this->viewport());
    painter.setPen(Qt::black);
    int x0=40, y0=300;
    //y坐标系
    painter.drawLine(x0,y0,40,30);
    painter.drawLine(38,32,40,30);
    painter.drawLine(40,30,42,32);
    painter.drawText(20,30,tr("销售数量"));
    for(int i=1;i<5;i++){
        painter.drawLine(-1,-i*50,1,-i*50);
        painter.drawText(-20,-i*50,tr("%1").arg(i*5));
    }
    //x坐标系
    painter.drawLine(x0,y0,450,300);
    painter.drawLine(448,298,450,300);
    painter.drawLine(448,302,450,300);
    painter.drawText(430,295,tr("品牌"));
    int pos=x0+20;
    int row;
    for(row=0; row<this->model()->rowCount(rootIndex()); row++){
        QModelIndex index=this->model()->index(row,0,rootIndex());
        QString dep=this->model()->data(index).toString();
        painter.drawText(pos,y0+15,dep);
        pos+=50;
    }
    int posN=x0+20;
    for(row=0; row<this->model()->rowCount(rootIndex()); row++){
        QModelIndex index=this->model()->index(row,1,rootIndex());
        int sell=this->model()->data(index).toInt();
        int width=10;
        QModelIndex colorIndex=this->model()->index(row,0,rootIndex());
        QColor color=QColor(this->model()->data(colorIndex,Qt::DecorationRole).toString());
        if(selections->isSelected(index))
            painter.setBrush(QBrush(color,Qt::Dense3Pattern));
        else
            painter.setBrush(QBrush(color));
        painter.drawRect(QRect(posN,y0-sell*10,width,sell*10));
        QRegion regionM(posN,y0-sell*10,width,sell*10);
        RegionList<<regionM;
        posN+=50;
    }
}
